﻿using _01_登录界面.Common;
using _01_登录界面.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace _01_登录界面.ViewModel
{
    public class LoginViewModel:NotifyBase
    {
        //账户密码验证码信息
        public LoginModel MyLoginModel { get; set; } = new LoginModel();
        //关闭窗口命令
        public CommandBase CloseWindowCommand { get; set; }

        //登录命令
        public CommandBase LoginCommand { get; set;}
        //错误信息
        private string error;

        public string Error
        {
            get { return error; }
            set { error = value; Changed(); }
        }

        public LoginViewModel() 
        {
            this.MyLoginModel = new LoginModel();
            //this.MyLoginModel.Name = "ABC";
            //this.MyLoginModel.PassWord = "abc";
            //this.MyLoginModel.Validation = "1234"; 
            this.CloseWindowCommand= new CommandBase();
            this.CloseWindowCommand.DoExcute = new Action<object>((o) =>
            {
                //将o转成window
                (o as Window).Close();
            });
            this.CloseWindowCommand.DoCanExcute = new Func<object, bool>((o) =>
            {
                return true;
            });

            this.LoginCommand = new CommandBase();
            this.LoginCommand.DoExcute = new Action<object>(ExcuteLogin) ;
            this.LoginCommand.DoCanExcute = new Func<object, bool>((o) => { return true; });


		}

        private void ExcuteLogin(object o)
        {
            this.Error = "";
            if (string.IsNullOrEmpty(MyLoginModel.Name))
            {
                this.Error = "用户名为空";
            }
		    else if (string.IsNullOrEmpty(MyLoginModel.PassWord))
			{
				this.Error = "密码为空";
			}
			else if (string.IsNullOrEmpty(MyLoginModel.Validation))
			{
				this.Error = "验证码为空";
			}
			else if (MyLoginModel.Name == "123" && MyLoginModel.PassWord == "123" && MyLoginModel.Validation == "1234")
			{
				this.Error = "登录成功";

                //跳转
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    (o as Window).DialogResult = true;

                }));

				GlobalValues.UserInfo.Name = MyLoginModel.Name;
                GlobalValues.UserInfo.Gender = 1;
			}
			else
            {
				this.Error = "用户名密码错误";
			}
		}
    }
}
